﻿using UnityEngine;
using UnityEngine.Animations;

namespace UFrame
{

    /// <summary>
    /// 特效状态机,2D序列帧播放完毕关闭特效
    /// </summary>
    public class EffectsSMB : StateMachineBehaviour
    {
        public bool isExitDisabled = false;
        public int loopCount;
        public float playTime;


        public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
        {
            base.OnStateEnter(animator, stateInfo, layerIndex);
        }

        public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
        {
            base.OnStateExit(animator, stateInfo, layerIndex);
            if (isExitDisabled)
            {
                animator.gameObject.SetActive(false);
            }
        }

        public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex, AnimatorControllerPlayable controller)
        {
            base.OnStateExit(animator, stateInfo, layerIndex, controller);
            if (isExitDisabled)
            {
                animator.gameObject.SetActive(false);
            }
        }

        public override void OnStateMachineExit(Animator animator, int stateMachinePathHash)
        {
            base.OnStateMachineExit(animator, stateMachinePathHash);
        }

        public override void OnStateMachineExit(Animator animator, int stateMachinePathHash, AnimatorControllerPlayable controller)
        {
            base.OnStateMachineExit(animator, stateMachinePathHash, controller);
        }

        public override void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
        {
            base.OnStateMove(animator, stateInfo, layerIndex);
        }

        public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
        {
            base.OnStateUpdate(animator, stateInfo, layerIndex);
            AnimatorStateInfo info = animator.GetCurrentAnimatorStateInfo(0);
            if (loopCount > 0)
            {
                if (info.normalizedTime / loopCount >= 1)
                {
                    animator.gameObject.SetActive(false);
                }
            }

            if (playTime > 0 && info.normalizedTime >= playTime)
            {
                animator.gameObject.SetActive(false);
                //Debug.Log(info.normalizedTime);
            }

        }
    }

}
